package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.SysTenantService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.SysTenantForm;
import com.youlai.boot.factory.model.query.SysTenantQuery;
import com.youlai.boot.factory.model.vo.SysTenantVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 租户基本信息前端控制层
 *
 * @author youlaitech
 * @since 2025-11-22 08:24
 */
@Tag(name = "租户基本信息接口")
@RestController
@RequestMapping("/api/v1/sys-tenant")
@RequiredArgsConstructor
public class SysTenantController  {

    private final SysTenantService sysTenantService;

    @Operation(summary = "租户基本信息分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant:query')")
    public PageResult<SysTenantVO> getSysTenantPage(SysTenantQuery queryParams ) {
        IPage<SysTenantVO> result = sysTenantService.getSysTenantPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增租户基本信息")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant:add')")
    public Result<Void> saveSysTenant(@RequestBody @Valid SysTenantForm formData ) {
        boolean result = sysTenantService.saveSysTenant(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取租户基本信息表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant:edit')")
    public Result<SysTenantForm> getSysTenantForm(
        @Parameter(description = "租户基本信息ID") @PathVariable Long id
    ) {
        SysTenantForm formData = sysTenantService.getSysTenantFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改租户基本信息")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant:edit')")
    public Result<Void> updateSysTenant(
            @Parameter(description = "租户基本信息ID") @PathVariable Long id,
            @RequestBody @Validated SysTenantForm formData
    ) {
        boolean result = sysTenantService.updateSysTenant(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除租户基本信息")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:sys-tenant:delete')")
    public Result<Void> deleteSysTenants(
        @Parameter(description = "租户基本信息ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sysTenantService.deleteSysTenants(ids);
        return Result.judge(result);
    }
}
